﻿// This code is released under the BSD license.
namespace PugLib.UnitTest
{
    #region

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading;
    using Extensions;
    using IO;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    #endregion

    /// <summary>
    ///This is a test class for FilesTest and is intended
    ///to contain all FilesTest Unit Tests
    ///</summary>
    [TestClass]
    public class FilesTest
    {
        /// <summary>
        ///A test for GetFilesOfTypeFromDirectory
        ///</summary>
        [TestMethod]
        public void GetFilesOfTypeFromDirectoryTest()
        {
            IEnumerable<string> filePatterns = new[] { "*.txt", ".jpg" };
            string rootDirectory = Path.GetTempPath();
            const SearchOption searchOption = SearchOption.TopDirectoryOnly;
            Files.GetFilesOfTypeFromDirectory(filePatterns, rootDirectory, searchOption);
        }

        /// <summary>
        ///A test for GetFilesOfTypeFromDirectories
        ///</summary>
        [TestMethod]
        public void GetFilesOfTypeFromDirectoriesTest1()
        {
            const string filePattern = ".txt";
            IEnumerable<string> directories = new[] { Path.GetTempPath(), Environment.CurrentDirectory };
            const SearchOption searchOption = SearchOption.TopDirectoryOnly;
            Files.GetFilesOfTypeFromDirectories(filePattern, directories, searchOption);
        }

        /// <summary>
        ///A test for GetFilesOfTypeFromDirectories
        ///</summary>
        [TestMethod]
        public void GetFilesOfTypeFromDirectoriesTest()
        {
            IEnumerable<string> filePatterns = new[] { "*.txt", ".jpg" };
            IEnumerable<string> directories = new[] { Path.GetTempPath(), Environment.CurrentDirectory };
            const SearchOption searchOption = SearchOption.TopDirectoryOnly;
            Files.GetFilesOfTypeFromDirectories(filePatterns, directories, searchOption);
        }

        /// <summary>
        ///A test for CreateFileGuid
        ///</summary>
        [TestMethod]
        public void CreateFileGuidTest()
        {
            Assert.IsNotNull(Files.CreateFileGuid());

            string fullPathToFile = Path.GetTempFileName();

            File.WriteAllText(fullPathToFile, "BackgroundFileDeleteTest");
            Thread.Sleep(1500);

            Assert.IsTrue(File.Exists(fullPathToFile));

            Files.BackgroundFileDelete(fullPathToFile);

            Thread.Sleep(5000);
            Assert.IsFalse(File.Exists(fullPathToFile));
        }

        /// <summary>
        ///A test for BackgroundFileDelete
        ///</summary>
        [TestMethod]
        public void BackgroundFileDeleteTest()
        {
            string fullPathToFile = Path.GetTempFileName();

            File.WriteAllText(fullPathToFile, "BackgroundFileDeleteTest");
            Thread.Sleep(1500);

            Assert.IsTrue(File.Exists(fullPathToFile));

            Files.BackgroundFileDelete(fullPathToFile);

            Thread.Sleep(5000);
            Assert.IsFalse(File.Exists(fullPathToFile));
        }

        /// <summary>
        ///A test for BackgroundFileDelete
        ///</summary>
        [TestMethod]
        public void BackgroundFilesDeleteTest()
        {
            List<string> files = new List<string>
                                     {
                                         Path.GetTempFileName(),
                                         Path.GetTempFileName(),
                                         Path.GetTempFileName(),
                                         Path.GetTempFileName()
                                     };
            files.ForEach(f => File.WriteAllText(f, "test"));

            files.ForEach(f => Assert.IsTrue(File.Exists(f)));
            Files.BackgroundFileDelete(files);
            Thread.Sleep(5000);

            files = null;
            Files.BackgroundFileDelete(files);
        }


        [TestMethod]
        public void StupidTCS()
        {
            const string fuckedVersion = "1.2.asdf";
            const string newValidVersion = "1.2.3";
            Version bad;
            Version good;
            if (!newValidVersion.TryParseVersion(out good))
            {
                throw new InvalidOperationException("invalid version string provided: " + newValidVersion);
            }

            if (!fuckedVersion.TryParseVersion(out bad))
            {
                throw new InvalidOperationException("invalid version string provided: " + fuckedVersion);
            }
        }


    }
}